package parcomb

import java.util.ArrayList

fun <T, R> List<T>.foldl(f: (R, T) -> R, s0: R): R {
    var s: R = s0
    for(i in this)
        s = f(s, i)
    return s
}

fun <T, R> List<T>.foldr(f: (T, R) -> R, s0: R): R {
    var s: R = s0
    for(i in this.indices.reversed())
        s = f(this[i], s)
    return s
}

fun <T, R> List<T>.map(f: (T) -> R): List<R> {
    var s = ArrayList<R>()
    for(i in this)
        s.add(f(i))
    return s
}

fun List<Char>.toString() : String {
    var sb = StringBuffer()
    for(i in this)
        sb.append(i)
    return sb.toString();
}

fun <T> List<T>.toEnd(fromIndex: Int) = this.subList(fromIndex, this.size)

fun asList<T>(vararg  ts: T): List<T> = ts.toList()

fun <T> T.cons(xs: List<T>): List<T> = asList<T>(this).plus(xs)

fun Any.x(): ((Int) -> Int) {
    return { it * 2 }
}